package com.netvitesse.nvconnect.test

import java.io.FileInputStream;
import java.util.ArrayList;

import org.openqa.selenium.By
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

import geb.*
import geb.junit4.*
import org.junit.Test

import org.junit.runner.*
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.openqa.selenium.firefox.FirefoxDriver





@RunWith(JUnit4)
class GebWithJUnitTest extends GebTest{

	//	File getReportDir() {
	//		new File("build/reports/geb-reports")
	//	}


	//	void toTestSth() {
	//
	//		FileInputStream fileInputStream = new FileInputStream(
	//				"c:\\Locator.xls");
	//		HSSFWorkbook testData = new HSSFWorkbook(fileInputStream);
	//		HSSFSheet testWorksheet = testData.getSheet("Sheet1");
	//		HSSFRow attributeRow = testWorksheet.getRow(0);
	//		HSSFRow valueRow = testWorksheet.getRow(1);
	//
	//		ArrayList<String> attributeList = new ArrayList<String>();
	//		ArrayList<String> valueList = new ArrayList<String>();
	//
	//		int M = 12; // M stands for the number of attributes
	//		int N = 11; // N stands for the number of elements you want to
	//		// select from the data
	//
	//		for (int i = 0; i < M; i++) {
	//			attributeList.add(attributeRow.getCell(i).getStringCellValue());
	//			valueList.add(valueRow.getCell(i).getStringCellValue());
	//		}
	//		WebDriverWait wait = new WebDriverWait(driver, 5)
	//		to LoginPage
	//		field_user.value("admin")
	//		field_psw.value("admin")
	//		login.button.click(NvConnectManagerPage)
	//		clickLink("Advanced search").link.click(AdvancedSearchPage)
	//
	//		for (int j = 1; j < M; j++) {
	//			CombinationGenerator combinator = new CombinationGenerator(
	//					attributeList.size(), j);
	//
	//			def indices1
	//			while (combinator.hasMore()) {
	//				indices1 = combinator.getNext()
	//
	//				for (i in 0..(indices1.size()-1)) {
	//					autofill(attributeList.get(indices1[i]),valueList.get(indices1[i])).go
	//				}
	//				search.button.click()
	//				//wait.sleep 400
	//				for (i in 0..(indices1.size()-1)) {
	//					autofill(attributeList.get(indices1[i]),"").go
	//				}
	//			}
	//		}
	//
	//
	//
	//	}
	//	@Test
	void createIPBX () {
		// Login
		to LoginPage
		field_user.value("admin")
		field_psw.value("admin")
		loginbtn.click(NvConnectManagerPage)
		//To Create IPBX
		clickLink("Administration"," ").link.click()
		clickLink("VoIP configuration"," ").link.click()
		clickLink("New IPBX","innermenu").link.click(NewIPBXPage)
		iPBXName.value("test1")
		hostName.value("192.168.16.161")
		login.value("system")
		password.value("password")
		button("Test Connection").button.click()
		waitFor({theTestMsgDiv.present})	    // Wait for Ajax Loading

		/////////////The following code has some design problems///////////////////

		waitFor({button("Create").button.present})

		button("Create").button.click()

		////////////////////////////////////////////////////////////////////////////
	}

	@Test
	void createUser () {
		// Login
		to LoginPage
		field_user.value("admin")
		field_psw.value("admin")
		loginbtn.click(NvConnectManagerPage)
		clickLink("Administration"," ").link.click()
		clickLink("Users"," ").link.click()
		clickLink("New User","innermenu").link.click(NewUserPage)
		login.value("tester123")
		fullName.value("Donald Trump")
		password.value("urhired")
		description.value("Become the new CEO")
//		button("Create").button.click()
	}

	@Test
	void createMSSQLDataStore () {

		// Login
		to LoginPage
		field_user.value("admin")
		field_psw.value("admin")
		loginbtn.click(NvConnectManagerPage)	
		clickLink("Administration"," ").link.click()
		clickLink("OneDirectory"," ").link.click()
		clickLink("Add a new Datastore","innermenu").link.click(NewDatastorePage)
		datastoreName.value("MS SQL 1")
		description.value("Only for testing")
		button("rdExternal").button.click()
		button("btnNext").button.click()
		clickLink("Microsoft SQL Server Datastore"," ").link.click(NewSQLDatastorePage)
		server.value("192.168.16.240")
		database.value("test")
		login.value("sa")
		password.value("netvitesse")
		button("btnTest").button.click()
		
		waitFor(10,0.5) {theText.present}  // Wait for Ajax Loading
		button("btnNext-0").button.click()
		waitFor(15,0.5) {theTab2Div.present}
		$("div#accordion-0").tableName = "IDMS"
		$("option", value:"IDMS").click()
		button("btnNext-1").button.click()
//		waitFor(5,0.5) {theText.present}
	}

	@Test
	void createMySQLDataStore () {

		// Login
		to LoginPage
		field_user.value("admin")
		field_psw.value("admin")
		loginbtn.click(NvConnectManagerPage)				
		clickLink("Administration"," ").link.click()
		clickLink("OneDirectory"," ").link.click()
		clickLink("Add a new Datastore","innermenu").link.click(NewDatastorePage)
		datastoreName.value("MySQL 1")
		description.value("Only for testing")
		button("rdExternal").button.click()
		button("btnNext").button.click()
		clickLink("MySQL Datastore"," ").link.click(NewSQLDatastorePage)
		server.value("netvitesse.net")
		login.value("sugarcrm")
		password.value("hzTabS54e")
		database.value("onedirectory")
		button("btnTest").button.click()   
		waitFor(10,0.5) {theText.present}    // Wait for Ajax Loading
//		button("btnNext-0").button.click()
		//
		
	}

	//	@Test
	//	void








}